package com.android.pagemarket.ui.activity.base;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.android.pagemarket.R;
import com.android.pagemarket.util.OnTabChangedListener;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-9-25
 * @see : Tab activity的基类
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public abstract class TabActivity extends ActivityGroup implements OnTabChangedListener
{   
    private PopupWindow    popwin;
    
    public static enum LAYOUT_TYPE
    {
        UP,BOTTOM,DEFINE
    }
    
    protected ViewGroup mTabContentLayout;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);                
        if(getLayoutType() == LAYOUT_TYPE.UP)
        {
            setContentView(R.layout.activity_tab_up);
        }else
        if(getLayoutType() == LAYOUT_TYPE.BOTTOM)
        {
            setContentView(R.layout.activity_tab_buttom);
        }else
        {
            // 由子类去设置
        }
    }
    
    @Override
    protected void onPause()
    {
        hidePopWin();
        super.onPause();
    }
    
    @Override
    public void setContentView(int layoutResID)
    {
        super.setContentView(layoutResID);
        mTabContentLayout = (ViewGroup)findViewById(R.id.tab_content);
        if(getLayoutType() != LAYOUT_TYPE.DEFINE)
        {
            setupTabView((LinearLayout)findViewById(R.id.tab_index));
        }
    }
    
    @SuppressWarnings("unchecked")
    protected <T extends View> T getViewById(int id)
    {
        View view = findViewById(id);
        return (T)view;
    }
    
    @SuppressWarnings("unchecked")
    protected <T extends View> T inflate(int res)
    {
        return (T)LayoutInflater.from(this).inflate(res, null);
    }
    
    protected LAYOUT_TYPE getLayoutType()
    {
        return LAYOUT_TYPE.UP;
    }
        
    /** 由子类去继承: 当使用默认布局UP或者BUTTOM时添加tab视图*/
    protected void setupTabView(LinearLayout tabIndexLay)
    {
        // to do 
    }
    
    protected void showTabContent(String id, Intent subWindows)
    {
        if(subWindows == null) return;
        Window localWindow = getLocalActivityManager().startActivity(id, subWindows);
        mTabContentLayout.removeAllViews();
        mTabContentLayout.addView(localWindow.getDecorView());
    }
    
    @Override
    public void onTabChanged(int id, Intent subWindows)
    {
        showTabContent(id +"", subWindows);
    }

    /** 持有PopWin*/
    public void setPopWin(PopupWindow popwin)
    {
        if(popwin != null)
        {
            this.popwin = popwin;
        }
    }
    
    /** 获取PopWin*/
    public PopupWindow getPopupWin()
    {
        return popwin;
    }
    
    /** 关闭PopWin*/
    public void hidePopWin()
    {
        if(popwin != null && popwin.isShowing())
        {
            popwin.dismiss();
        }
    }
}
